package com.hongyue;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws DocumentException {
        //创建架包对象
        SAXReader reader = new SAXReader();
        //给架包指导路径
        Document document = reader.read(new File("dey17/xml/xml.xml"));
        //获取根标签
        Element rootElement = document.getRootElement();
        Element heima = rootElement.element("heima");
        //根据根标签获取标签集合
        List<Element> elements = heima.elements("java");
        ArrayList<Student> list = new ArrayList<>();
        //遍历标签集合获取内容
        for (Element element : elements) {
            //获取id
            Attribute attributeid = element.attribute("ID");
            String id = attributeid.getValue();
            //获取内容
            Element elementminzi = element.element("minzi");
            String mingzi = elementminzi.getText();

            Element elementnianl = element.element("nianl");
            String nianl = elementnianl.getText();

            Element elementzhiye = element.element("zhiye");
            String zhiye = elementzhiye.getText();

            list.add(new Student(id, mingzi, nianl, zhiye));
        }
        for (Student student : list) {
            System.out.println("student = " + student);
        }
    }
}
